#ifndef xpack_graphic_draw_polygen
#define xpack_graphic_draw_polygen
#pragma push_macro("xuser")
#undef  xuser
#define xuser mixc::graphic_draw_polygen::inc
#include"define/base_type.hpp"
#include"graphic/draw/line.hpp"
#include"graphic/private/rander_helper.hpp"
#include"macro/xexport.hpp"
#include"math/point.hpp"
#include"interface/unified_seq.hpp"
#pragma pop_macro("xuser")

namespace mixc::graphic_draw_polygen::inc{
    template<class seq_t, class rander_t>
    inline void polygen_core(inc::pt2wx start, seq_t const & point_list, rander_t const & rander){
        // 多边形至少需要三条边，和 polyline 不同
        if (point_list->length <= uxx(2)){
            return;
        }

        for(uxx i = 0, length = point_list->length; i < length; i++){
            auto i_end          = i + 1 < length ? i + 1 : 0;
            auto begin          = point_list[i](start->x, start->y);
            auto end            = point_list[i_end](start->x, start->y);
            inc::draw::line(begin, end, rander);
        }
    }
}

namespace mixc::graphic_draw_polygen::origin{
    template<class seq_t, class rander_t>
    requires(inc::can_unified_seqlizex<seq_t, inc::pt2wx>)
    inline void polygen(inc::pt2wx start, seq_t && point_list, rander_t const & rander){
        if (inc::unified_seq<seq_t> list(point_list);
            inc::rander_helper::get_texture_mode(rander) != inc::texture_mode_t::none){
            inc::rander_helper::texturing(rander, [&](auto const & randerx){
                inc::polygen_core(start, list, randerx);
            });

            // 放到后边，以便遮挡多余的纹理部分
            inc::polygen_core(start, list, rander);
        }
        else{
            inc::polygen_core(start, list, rander);
        }
    }

    template<class seq_t, class rander_t>
    requires(inc::can_unified_seqlizex<seq_t, inc::pt2wx>)
    inline void polygen(seq_t && point_list, rander_t const & rander){
        inc::unified_seq<seq_t> list(point_list);
        inc::polygen_core({}, list, rander);
    }
}
#endif

xexport_spacex(draw, mixc::graphic_draw_polygen::origin)
